package com.tonann.app.service.impl;

import com.tonann.app.domain.AppFaultRule;
import com.tonann.app.mapper.AppFaultRuleMapper;
import com.tonann.app.service.IAppFaultRuleService;
import com.tonann.app.utils.FaultRuleUtils;
import com.tonann.common.utils.DateUtils;
import com.tonann.common.utils.SecurityUtils;
import com.tonann.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 故障诊断规则Service业务层处理
 *
 * @author tonann
 * @date 2022-10-15
 */
@Service
public class AppFaultRuleServiceImpl implements IAppFaultRuleService
{
    @Resource
    private AppFaultRuleMapper appFaultRuleMapper;

    /**
     * 查询故障诊断规则
     *
     * @param faultRuleId 故障诊断规则主键
     * @return 故障诊断规则
     */
    @Override
    public AppFaultRule selectAppFaultRuleByFaultRuleId(Long faultRuleId) {
        AppFaultRule faultRule = FaultRuleUtils.getFaultRuleByFaultRuleId(faultRuleId);
        if (StringUtils.isNull(faultRule)) {
            faultRule = appFaultRuleMapper.selectAppFaultRuleByFaultRuleId(faultRuleId);
            if (faultRule != null) {
                FaultRuleUtils.setFaultRuleByFaultRuleId(faultRule);
            }
        }
        return faultRule;
    }

    /**
     * 查询故障诊断规则列表
     *
     * @param appFaultRule 故障诊断规则
     * @return 故障诊断规则
     */
    @Override
    public List<AppFaultRule> selectAppFaultRuleList(AppFaultRule appFaultRule)
    {
        List<AppFaultRule> appFaultRuleList = appFaultRuleMapper.selectAppFaultRuleList(appFaultRule);
        FaultRuleUtils.setFaultRuleCache("list", appFaultRuleList);
        return appFaultRuleList;
    }

    /**
     * 新增故障诊断规则
     *
     * @param appFaultRule 故障诊断规则
     * @return 结果
     */
    @Override
    public int insertAppFaultRule(AppFaultRule appFaultRule) {
        appFaultRule.setCreateTime(DateUtils.getNowDate());
        appFaultRule.setUpdateTime(DateUtils.getNowDate());
        int row = appFaultRuleMapper.insertAppFaultRule(appFaultRule);
        if (row > 0) {
            FaultRuleUtils.setFaultRuleByFaultRuleId(appFaultRule);
            List<AppFaultRule> appFaultRuleList = appFaultRuleMapper.selectAppFaultRuleList(appFaultRule);
            FaultRuleUtils.setFaultRuleCache("list", appFaultRuleList);
        }
        return row;
    }

    /**
     * 修改故障诊断规则
     *
     * @param appFaultRule 故障诊断规则
     * @return 结果
     */
    @Override
    public int updateAppFaultRule(AppFaultRule appFaultRule) {
        appFaultRule.setUpdateTime(DateUtils.getNowDate());
        int row = appFaultRuleMapper.updateAppFaultRule(appFaultRule);
        if (row > 0) {
            FaultRuleUtils.setFaultRuleByFaultRuleId(appFaultRule);
            List<AppFaultRule> appFaultRuleList = appFaultRuleMapper.selectAppFaultRuleList(appFaultRule);
            FaultRuleUtils.setFaultRuleCache("list", appFaultRuleList);
        }
        return row;
    }

    /**
     * 批量删除故障诊断规则
     *
     * @param faultRuleIds 需要删除的故障诊断规则主键
     * @return 结果
     */
    @Override
    public int deleteAppFaultRuleByFaultRuleIds(Long[] faultRuleIds) {
        int row = 0;
        for (Long faultRuleId : faultRuleIds) {
            row = appFaultRuleMapper.deleteAppFaultRuleByFaultRuleId(faultRuleId);
            FaultRuleUtils.removeFaultRuleCache("faultRuleId:" + faultRuleId);
            List<AppFaultRule> appFaultRuleList = appFaultRuleMapper.selectAppFaultRuleList(new AppFaultRule());
            FaultRuleUtils.setFaultRuleCache("list", appFaultRuleList);
        }
        return row;
    }

    /**
     * 删除故障诊断规则信息
     *
     * @param faultRuleId 故障诊断规则主键
     * @return 结果
     */
    @Override
    public int deleteAppFaultRuleByFaultRuleId(Long faultRuleId) {
        int row = appFaultRuleMapper.deleteAppFaultRuleByFaultRuleId(faultRuleId);
        FaultRuleUtils.removeFaultRuleCache("faultRuleId:" + faultRuleId);
        List<AppFaultRule> appFaultRuleList = appFaultRuleMapper.selectAppFaultRuleList(new AppFaultRule());
        FaultRuleUtils.setFaultRuleCache("list", appFaultRuleList);
        return row;
    }

    @Override
    public List<AppFaultRule> selectAppFaultRuleByFaultRuleName(String faultRuleName) {
        List<AppFaultRule> faultRuleList = FaultRuleUtils.getFaultRuleCache("name:" + faultRuleName);
        if (StringUtils.isNull(faultRuleList) || faultRuleList.size() == 0) {
            faultRuleList = appFaultRuleMapper.selectAppFaultRuleByFaultRuleName(faultRuleName);
            FaultRuleUtils.setFaultRuleCache("name:" + faultRuleName, faultRuleList);
        }
        return faultRuleList;
    }
}
